<template>
  <HeaderLayout
    title = '会议锦集'

    @scroll="handleScroll"
  >
    <div :class="{ tool: true, active: isPicker }">
      <div class="tool-item tool-cancel" @click="handleToolCancel">取消</div>
      <div class="tool-item tool-ok" @click="handleSubmit">确定拼图（{{ pickerImage.length }} / {{ pickerLimit }}）</div>
    </div>

    <Banner :height="143" />

    <div class="nav" ref="nav">
      <div class="nav-album">
        <h1>会议集锦口号或标题</h1>
        <div class="nav-browser">
          <Icon name = 'password-view' />12396
        </div>
      </div>
      <div class="nav-tool">
        <!-- 统计 -->
        <div class="nav-tool-item">
          <div class="nav-tool-title">
            <Icon name = 'photo' />全部图片
          </div>
          <div class="nav-tool-value">228张</div>
        </div>
        <!-- 拼图 -->
        <div class="nav-tool-item" @click="handleJigsaw">
          <div class="nav-tool-title">
            <Icon name = 'exchange' />拼图
          </div>
        </div>
        <!-- 时间轴 -->
        <div class="nav-tool-item" @click="handleTimeline">
          <div class="nav-tool-title">
            <Icon name = 'clock' />开启时间轴
          </div>
        </div>
      </div>
    </div>

    <div :class="{ nav: true, 'nav-fixed': true, active: visibleFixedNav }" slot='layout-slot'>
      <div class="nav-album">
        <h1>会议集锦口号或标题</h1>
        <div class="nav-browser">
          <Icon name = 'password-view' />12396
        </div>
      </div>
      <div class="nav-tool">
        <!-- 统计 -->
        <div class="nav-tool-item">
          <div class="nav-tool-title">
            <Icon name = 'photo' />全部图片
          </div>
          <div class="nav-tool-value">228张</div>
        </div>
        <!-- 拼图 -->
        <div class="nav-tool-item" @click="handleJigsaw">
          <div class="nav-tool-title">
            <Icon name = 'exchange' />拼图
          </div>
        </div>
        <!-- 时间轴 -->
        <div class="nav-tool-item" @click="handleTimeline">
          <div class="nav-tool-title">
            <Icon name = 'clock' />开启时间轴
          </div>
        </div>
      </div>
    </div>

    <div class="content">
      <Row :gutter="2" v-if="!isTimeline">
        <Col :span="8" v-for="(v, i) in data" :key="i">
          <div
            class="img-wrap"
            @click="handlePickerImage(v, i, $event)"
          >
            <img :src="v.msrc" />
            <div
              v-if="isPicker"
              :class="{ 'img-checkbox': true, 'img-checkbox-selected': pickerImage.some(_v => _v === v.id  )}"
            >
            </div>
          </div>
        </Col>
      </Row>
      
      <div v-for="(v, i) in timelineData" :key="i" v-if="isTimeline">
        <div class="timeline-date">
          {{ formateDate(v.startDate, v.endDate) }}
        </div>
        <Row :gutter="2">
          <Col :span="8" v-for="(_v, _i) in v.data" :key="_i">
            <div
              class="img-wrap"
              @click="handlePickerImage(_v, _i, $event)"
            >
              <img :src="_v.src" />
              <div
                v-if="isPicker"
                :class="{ 'img-checkbox': true, 'img-checkbox-selected': pickerImage.some(__v => __v === _v.id  )}"
              >
              </div>
            </div>
          </Col>
        </Row>
      </div>

      <div class="bottom-text" v-if="loading">
        <Loading size = '20px' />
        加载中
      </div>

      <div class="bottom-text" v-if="isEnd">没有数据啦</div>
    </div>
    <Popup v-model="visibleJigsawModal">
      <div class="jigsaw-preview">
        <img :src="JigsawSrc" />
      </div>
    </Popup>
  </HeaderLayout>
</template>

<script>

import _ from 'lodash'
import moment from 'moment'
import { Icon, Row, Col, Loading, Popup, Toast } from 'vant'
import HeaderLayout from '@/layout/headerLayout/index.vue'
import Banner from '@/component/banner/index.vue'
import { mapState, mapMutations } from 'vuex'

// 生成模拟数据
function fakeData (start = 0) {
  return [...Array(12)].map((v, i) => ({
    id: start + i + 1,
    src: 'https://farm6.staticflickr.com/5591/15008867125_68a8ed88cc_b.jpg',
    msrc: 'https://farm6.staticflickr.com/5591/15008867125_68a8ed88cc_m.jpg',
    alt: 'picture1',
    title: 'Image Caption ' + (i + 1),
    w: 600,
    h: 400
  }))
}

export default {

  name: 'index',

  data () {
    return {
      // 拼图模态框
      visibleJigsawModal: false,
      // 拼图地址
      JigsawSrc: '',

      loading: false,
      isEnd: false,

      // 是否是时间轴浏览模式
      isTimeline: false,
      // 是否是选择图片的状态（拼图）
      isPicker: false,
      // 可选择图片的最大张数
      pickerLimit: 9,
      // 选择的图片
      pickerImage: [],

      // 正常模式图片数据
      data: [],
      // 时间轴模式图片数据
      timelineData: [],

      // 磁铁工具栏
      visibleFixedNav: false
    }
  },

  computed: {
    // 是否可以继续选择图片
    canPicker: function () {
      return this.pickerImage.length < this.pickerLimit
    },
  },

  created: function () {
    this.fakeData()
  },

  methods: {
    ...mapMutations(['showGlobalPhoto']),

    // 模拟数据
    fakeData: function () {
      if (this.loading || this.isEnd) {
        return
      }

      this.loading = true

      setTimeout(() => {
        this.loading = false

        if (this.isTimeline) {
          this.timelineData = this.timelineData.concat({
            startDate: new Date().getTime(),
            endDate: new Date().getTime(),
            data: fakeData(this.timelineData.length)
          })

          if (this.timelineData.length > 50) {
            this.isEnd = true
          }

          return
        }

        this.data = this.data.concat(fakeData(this.data.length))

        if (this.data.length > 50) {
          this.isEnd = true
        }
      }, 1000)
    },
    // 时间轴状态切换
    handleTimeline: function () {
      if (!this.isTimeline) {
        this.isTimeline = true
      } else {
        this.isTimeline = false
      }

      this.fakeData()
      // console.log(this.isTimeline)
    },
    handleJigsaw: function () {
      if (this.isPicker) {
        this.isPicker = false
        this.pickerImage = []
      } else {
        this.isPicker = true
      }
    },
    // 拼图
    handleSubmit: function () {
      if (this.pickerImage.length < 2) {
        Toast('拼图数量至少两张')

        return
      }

      Toast.loading({
        duration: 0,       // 持续展示 toast
        forbidClick: true, // 禁用背景点击
        mask: true,
        message: '拼图中...'
      })

      setTimeout(() => {
        
        const img = new Image()
        const src = 'http://static1.pailixiang.com/puzzle/1809/18091520194832341416.jpg'

        img.onload = () => {
          Toast.clear()
          this.JigsawSrc = src
          this.visibleJigsawModal = true
        }

        img.src = src

      }, 2000)
    },
    // 正常状态下是预览图片
    // 拼图状态下是选择图片
    handlePickerImage: function (v, i, e) {
      if (!this.isPicker) {
        // 打开模态框
        this.showGlobalPhoto({
          data: this.data,
          options: {
            index: i
          }
        })

        return
      }
  
      if (this.pickerImage.some(_v => _v === v.id)) {
        this.pickerImage = this.pickerImage.filter(_v => _v !== v.id)
      } else {
        if (this.canPicker) {
          this.pickerImage.push(v.id)
        }
      }
    },
    handleToolCancel: function () {
      this.isPicker = false
    },
    // 节流
    handleScroll: _.throttle(function (e) {
      const {
        scrollTop,
        clientHeight,
        scrollHeight,
      } = e.target

      const {
        offsetTop
      } = this.$refs.nav

      // 这里是代码是控制顶部菜单显示
      if (scrollTop >= offsetTop) {
        if (!this.visibleFixedNav) {
          this.visibleFixedNav = true
        }
      } else {
        if (this.visibleFixedNav) {
          this.visibleFixedNav = false
        }
      }

      // 这里的代码控制下拉加载
      if (scrollTop + clientHeight + 100 >= scrollHeight) {
        this.fakeData()
      }
    }, 100),
    formateDate: function (startDate, endDate) {
      let res = ''

      const _start = moment(startDate).format('YYYY年MM月DD日 hh:ss')
      const _end = moment(endDate).format('YYYY年MM月DD日 hh:ss')

      res = _start + ' ~ ' + _end

      return res
    }
  },

  components: {
    HeaderLayout,
    Banner,
    Icon,
    Row,
    Col,
    Loading,
    Popup,
  }
}

</script>

<style lang="less" scoped>

@import '../../less/mixin.less';

.nav {
  margin-top: -5px;

  &-album {
    height: 35px;
    background-color: #f3f6f8;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 15px;
    min-width: 0;

    h1 {
      margin: 0;
      padding: 0;
      font-weight: 400;
      font-size: 13px;
      color: #232323;

      .text-overflow();
    }

    .nav-browser {
      font-size: 12px;
      color: #FFAC00;
      flex-shrink: 1;
      margin-left: 10px;
      white-space: nowrap;
      font-weight: 500;

      i {
        font-weight: normal;
        color: #666;
        display: inline-block;
        margin-right: 3px;
        margin-right: 5px;
        font-size: 16px;
        vertical-align: -2px;
      } 
    }
  }

  &-tool {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #fff;
    height: 38px;

    &-item {
      flex-shrink: 0;
      display: flex;
      align-items: center;
      height: 100%;
      justify-content: space-between;
      padding: 0 17px;
      font-size: 13px;
      color: #4C4C4C;
      position: relative;
      transition: background-color .2s;
      background-color: transparent;

      &.active,
      &:active {
        background-color: rgba(0,0,0,.1);
      }

      &:after {
        content: '';
        position: absolute;
        right: 0;
        width: 1px;
        height: 14px;
        top: 50%;
        margin-top: -7px;
        background-color: #d9d9d9;
      }

      &:last-child:after {
        display: none;
      }

      &:first-child {
        flex-grow: 1;
      }

      .nav-tool-title {
        i {
          color: #666;
          display: inline-block;
          margin-right: 5px;
          font-size: 14px;
          vertical-align: -2px;
        }
      }
      .nav-tool-value {
        font-size: 12px;
        color: #A2A2A2;
      }
    }
  }

  &-fixed {
    display: none;
    position: absolute;
    top: 50px;
    left: 0;
    right: 0;
    z-index: 100;

    &.active {
      display: block;
    }
  }
}

.content {
  overflow-x: hidden;

  /deep/ .van-col--8 {
    margin-top: 2px;

    &:nth-child(1),
    &:nth-child(2),
    &:nth-child(3) {
      margin-top: 0;
    }
  }

  .img-wrap {
    position: relative;
    padding-bottom: 100%;
    overflow: hidden;
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center center;

    img {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: opacity .2s;
      // opacity: 0;

      &[lazy="loaded"] {
        opacity: 1;
      }
    }

    .img-checkbox {
      position: absolute;
      right: 5px;
      top: 5px;
      width: 26px;
      height: 26px;
      border-radius: 18px;
      border: 2px solid #fff;
      background-color: rgba(0,0,0,.7);
      transition: transform .2s, opacity .2s;
      outline: none;
      z-index: 10;

      &:after {
        content: '';
        position: absolute;
        width: 14px;
        height: 9px;
        border-bottom: 3px solid #fff;
        border-left: 3px solid #fff;
        transform: rotate(-45deg);
        top: 5px;
        left: 4px;
        display: none;
      }

      &-selected {
        background-color: #3BBF51;

        &:after {
          display: block;
        }
      }
    }
  }
}

.tool {
  display: flex;
  height: 42px;
  transition: transform .3s, opacity .3s;
  opacity: 0;
  transform: translate(0, 100%);
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  // visibility: hidden;
  z-index: 100;

  &.active {
    // visibility: visible;
    transform: translate(0, 0);
    opacity: 1;
  }

  &-item {
    text-align: center;
    line-height: 42px;
    height: 42px;
    flex: 1 0 0;
    padding: 0 10px;
    font-size: 14px;
    color: #fff;
    background-color: rgba(0,0,0,.9);

    .text-overflow();
  }

  &-ok {
    background-color: rgba(59, 191, 81, .9);
  }
}

.timeline-date {
  padding: 15px 10px;
  // background-color: #fff;
  font-size: 14px;
}

.bottom-text  {
  margin: 20px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #999;

  /deep/ .van-loading {
    margin-right: 10px;
  }
}

.jigsaw-preview {
  width: 300px;
  padding: 10px;
}

</style>